package com.fwj.common.annotation;

import com.alibaba.fastjson.JSONObject;
import com.fwj.user.entity.user.UserBaseInfo;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import java.lang.annotation.*;

/**
 * @Author: fwj
 * @Data: 2022/4/16 0:59
 * @Version 1.0
 * @Description: 安全附件下载拦截校验注解
 */

@Documented
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.METHOD,ElementType.FIELD,ElementType.PARAMETER})
public @interface DownloadPermission {


    @Component
    @Aspect
    class DownloadAccessValidator {

        @Pointcut(value = "@annotation(com.fwj.common.annotation.DownloadPermission)")
        private void pointCut(){

        }

        @Before("pointCut() && @annotation(downloadPermission)")
        public void before(JoinPoint joinPoint,DownloadPermission downloadPermission){
            // 全局获取 request
            HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
            Object[] args = joinPoint.getArgs();
            JSONObject jsonObject = JSONObject.parseObject(args[0].toString());
            UserBaseInfo userBaseInfo = JSONObject.parseObject(JSONObject.toJSONString(args[1]),UserBaseInfo.class);
        }

    }
}
